Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.Loader.setConfig({
        enabled:true
    });
    Ext.application({
        name: 'MyApp',
        //extend: 'MyApp.Application',
        appForder:'app',
        //autoCreateViewport:true,
        //requires:['MyApp.view.Login'],
        refs:[{ref: 'login',
        selector: 'login'
        },{ref: 'viewport',
        selector: 'viewport'
        },//{ref: 'signin',selector: 'signin'}
        ],
        launch: function() {
            // This is fired as soon as the page is ready
            console.log('helo');
            Ext.create('Ext.container.Viewport',{
                layout: 'auto',
                items:[
                    {
                        //title:'hello',

                    },{
                        xtype:'panel',
                        layout: 'anchor',
                        title: 'Login',
                        width:300,
                        height: 180,
                        bodyPadding: 5,
                        defaultType: 'textfield',
                        items: [{
                            fieldLabel: '用户名',
                            id:'username',
                            name: 'username',
                            maxLength:25,
                            value:'aaaa',
                            allowBlank: false
                        },{
                            fieldLabel: '密码',
                            id:'password',
                            name: 'password',
                            inputType: 'password',
                            minLength:6,
                            maxLength:25,
                            allowBlank: false
                        },
                        // {
                        //     fieldLabel: '确认密码',
                        //     id:'qrpassword',
                        //     name: 'qrpassword',
                        //     inputType: 'password',
                        //     minLength:6,
                        //     maxLength:25,
                        //     allowBlank: false
                        // },
                        {xtype:'checkbox',
                           boxLabel: '记住密码',
                           name:'topping',
                           inputValue: '1',
                           id: 'checkbox1'
                        },{xtype: 'button', 
                            text: '注册',
                            id:'mysignin',
                            width:70,
                            arrowAlign: 'right',
                            margin:'-45 0 0 180',
                            listeners: {
                                click: function(btn, url){
                                    // alert(btn.text);
                                    // console.log(url.target.baseURI);
                                    // var mywin = Ext.getDom('mywin');
                                    // console.log(mywin);
                                    // console.log(mywin.parentNode);
                                    //mywin.append('a',true);
                                    var mywin_a = Ext.getDom('mywin_a');
                                    var strurl = mywin_a.baseURI;
                                    var iindex = strurl.lastIndexOf('/login');
                                    var s1 = strurl.slice(0, iindex + 1) + 'signin';
                                    console.log('重定向页面' + s1);
                                    document.location.href = s1;
                                    //console.log(mywin_a);
                                    //console.log(mywin_a.href);
                                    //console.log(mywin_a.href+'/signin');
                                    //document.location.href = mywin_a.href+'/signin';
                                    //mywin.parentNode.location.href = 'www.xsgl.com/admin/signin';
                                    //doucument.location.href = 'www.xsgl.com/admin/signin';
                                }
                            }
                        }],
                        buttons:[{
                            text:'忘记密码',
                            id:'forgetpwd',
                            name:'forgetpwd',
                            handler:function(){
                                console.log('forgetpwd');
                            }
                        },{
                            text:'重置',
                            id:'reset',
                            name:'reset',
                            handler:function(btn){
                                console.log(btn.ownerCt.ownerCt.queryById('username'));
                                console.log('reset');
                                console.log(Ext.get('username'));
                                console.log(Ext.get('username').id);
                                console.log(btn.ownerCt.ownerCt.queryById('username').value);
                                //console.log(Ext.get('username').getValue());
                                //Ext.get('username').value = '';
                        }},{
                            text:'提交',
                            id:'submit',
                            name:'submit',
                            handler:function(btn){
                                console.log('submit');
                                var username = btn.ownerCt.ownerCt.queryById('username').value;
                                var password = btn.ownerCt.ownerCt.queryById('password').value;
                                //var qrpassword = btn.ownerCt.ownerCt.queryById('qrpassword').value;
                                // if(password !== qrpassword){
                                //     Ext.Msg.alert('注意','两次密码输入不一致');
                                // }else{

                                    var mywin_a = Ext.getDom('mywin_a');
                                    var sturl = mywin_a.baseURI;
                                    /*var suburl = sturl.substring(sturl.lastIndexOf('/')+1,);
                                    console.log('sturl:'+sturl);
                                    console.log('strul:last:'+sturl.substring(sturl.lastIndexOf('/')+1,));
                                    var iurl = null;
                                    if(suburl === 'admin'){
                                        iurl = suburl +'/';//suburl +'/login';
                                    }else{
                                        iurl = sturl;
                                    }*/
                                    Ext.Ajax.request({
                                        //url: 'page.php',
                                        //url: 'login',
                                        url: sturl,
                                        timeout: 60000,
                                        cors:true,
                                        method: 'POST',
                                        params: {
                                            id: 1,
                                            username: username,
                                            password: password,
                                            //qrpassword: qrpassword
                                        },
                                        success: function(response, opts){
                                            //var text = Ext.decode(response.responseText);
                                            var text = response.responseText;
                                            // process server response here
                                            //console.dir(text);
                                            console.log(response.responseText);
                                            console.log(response.status);

                                            var retarray = Ext.decode(response.responseText);
                                            /*获取用户返回信息,判断是教师用户还是学生用户
                                            * 根据用户类型定向到不同页面
                                            * */
                                            //获取用户类型
                                            console.log(retarray['data'][0]);
                                            //页面重定向
                                            var mywin_a = Ext.getDom('mywin_a');
                                            var strurl = mywin_a.baseURI;
                                            var iindex = strurl.lastIndexOf('/login');

                                            if('student' === retarray['data'][0]) {
                                                var s1 = strurl.slice(0, iindex + 1) + 'user.html';
                                                console.log('重定向页面' + s1);
                                            }
                                            if('teacher' === retarray['data'][0]){
                                                var s1 = strurl.slice(0, iindex + 1) + 'teacher.html';
                                                console.log('重定向页面' + s1);
                                                document.location.href = s1;
                                            }
                                            //document.location.href = s1 + iurl + 'user';
                                            /*console.log('iurl:'+iurl);*/
                                            /*if(iurl){
                                                var mywin_a = Ext.getDom('mywin_a');
                                                var strurl = mywin_a.baseURI;
                                                var iindex = strurl.lastIndexOf('/admin');
                                                var s1 = strurl.slice(0,iindex+1);
                                                console.log(s1);
                                                document.location.href = s1 + iurl + 'user';
                                            }*/
                                            // else{
                                            //     var mywin_a = Ext.getDom('mywin_a');
                                            //     var strurl = mywin_a.baseURI;
                                            //     var iindex = strurl.lastIndexOf('/login');
                                            //     var s1 = strurl.slice(0,iindex+1);
                                            //     document.location.href = s1 + '/user';
                                            // }
                                            
                                            //url为admin/login时
                                            // var mywin_a = Ext.getDom('mywin_a');
                                            // var strurl = mywin_a.baseURI;
                                            // var iindex = strurl.lastIndexOf('/login');
                                            // var s1 = strurl.slice(0,iindex+1);
                                            // //var strurl2 = s1 + '/user';
                                            // document.location.href = s1 + '/user';
                                            //document.location.href = mywin_a.href+'/user';
                                            //console.log(strurl);
                                            //console.log(strurl2);  <div class="info"><h1>密码不能少于6个字符</h1></div>

                                        },
                                        failure: function(response, opts) {
                                            //var patt = /<h1>(.*)<\/h1>/;
                                            //console.log(patt.test("<h1>密码不能少于6个字符</h1>"));
                                            //var n = /<h1>(.*)<\/h1>/.exec("<h1>密码不能少于6个字符</h1>");
                                            //console.log(n);
                                            //var n = str.search(/<div class="info"><h1>(.*)</h1>/);
                                            //console.log(response.responseText);

                                            //验证用户名密码
                                            var validatetxt1 = /<h1>(.*)<\/h1>/.exec(response.responseText);
                                            if(!Ext.isEmpty(validatetxt1)){
                                                Ext.Msg.alert('提示信息',validatetxt1[1]);
                                                console.log(validatetxt1[1]);
                                            }
                                            console.log(response.responseText);
                                            console.log('server-side failure with status code ' + response.status);
                                        }
                                    });
                                //}
                            }
                        }],
                        renderTo: Ext.getBody()
                    }
                ]
            })
            
        },
        controllers:[]
        //views:['Signin']
    });
});